#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <linux/fb.h>
 #include <sys/mman.h>
#include <sys/ioctl.h>
enum Color{
   RED = 0x00ff0000, //红色
   GREEN = 0x0000ff00, //绿色
   BLACK = 0x00000000, //黑色
   WHITE = 0x00ffffff, //白色
   BLUE = 0x000000ff, //蓝色
   YELLOW = 0x00ffff00, //黄色
   
};

struct fb_var_screeninfo varinfo;

int main()
{
    //1、打开液晶屏
    int lcdFd = open("/dev/fb0",O_RDWR);
    if(lcdFd == -1)
    {
        perror("open lcd error");
        return -1;
    }
    //2、获取液晶屏设备的硬件参数
    if(ioctl(lcdFd, FBIOGET_VSCREENINFO, &varinfo) != 0)
    {
        perror("获取LCD设备可变属性信息失败");
        return -1;
    }
    //800   480
    printf("[可见区分辨率]: %d×%d\n", varinfo.xres, varinfo.yres);
    printf("[虚拟区分辨率]: %d×%d\n", varinfo.xres_virtual, varinfo.yres_virtual);
    printf("[色深]: %d bits\n", varinfo.bits_per_pixel); //32位   转成4个字节 32/8  
    //3、进行内存映射
    int(*lcd_p)[800] = (int(*)[800])mmap(NULL,varinfo.xres*varinfo.yres*varinfo.bits_per_pixel/8,PROT_READ|PROT_WRITE,MAP_SHARED,lcdFd,0);
    if(lcd_p == MAP_FAILED)
    {
        perror("mmap error");
        return -1;
    }
    //int lcd_p[480][800]
    
    //4、进行画图操作
    for(int j=0;j<480;j++)
    {
        for(int i=0;i<800;i++)
        {
            if(i>800/4 && i<800/4*3 && j>480/4 && j<480/4*3)
            {
                lcd_p[j][i] = RED;
            }
            else
            {
                lcd_p[j][i] = BLACK;
            }
        }
    }

    //解除内存映射
    munmap(lcd_p, varinfo.xres*varinfo.yres*varinfo.bits_per_pixel/8);

}